package com.example.practice;

import java.util.ArrayList;
import java.util.List;

import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.example.practice.adapter.StudyAdapter;

public class StudyActivity extends ActionBarActivity {
	private ViewPager viewPager;
	private StudyAdapter studyAdapter;
	private LinearLayout dotLayout;
	private List<Drawable> imageViews = new ArrayList<Drawable>();
	//�ײ�Բ��
	private List<ImageView> dotViews = new ArrayList<ImageView>(); 
 	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_study);
		viewPager = (ViewPager)findViewById(R.id.viewPager);
		dotLayout = (LinearLayout)findViewById(R.id.linear);
		
		initViewPager();
		initDots();
		updateDots();
	}
	
	private void initViewPager(){
	
		try {
			imageViews.add(new BitmapDrawable(BitmapFactory.decodeStream(getAssets().open("1.png"))));
			imageViews.add(new BitmapDrawable(BitmapFactory.decodeStream(getAssets().open("2.png"))));
			imageViews.add(new BitmapDrawable(BitmapFactory.decodeStream(getAssets().open("3.png"))));
		} catch (Exception e) {
			e.printStackTrace();
		}


		
		studyAdapter = new StudyAdapter(this,imageViews);
		viewPager.setAdapter(studyAdapter);
		
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				updateDots();
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	private void initDots(){
		dotViews.add(new ImageView(this));
		dotViews.add(new ImageView(this));
		dotViews.add(new ImageView(this));
		for(ImageView imageView : dotViews){
			dotLayout.addView(imageView);
		}

	}
	
	//更新dot
	private void updateDots(){
		for(ImageView imageView: dotViews){
			imageView.setBackgroundResource(R.drawable.dot_off);
		}
		
		if(dotViews.size() > 0)
		dotViews.get(viewPager.getCurrentItem()%dotViews.size()).setBackgroundResource(R.drawable.dot_on);
		
	}
	

	

	
}
